package com.example.demo.mapper;

import com.example.demo.model.Coffee;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface CoffeeMapper {
//    @Select("select * from coffee order by id")
//    List<Coffee> findAllWithRowBounds(RowBounds rowBounds);
//
//    @Select("select * from coffee order by id")
//    List<Coffee> findAllWithParam(@Param("pageNum") int pageNum,
//                                  @Param("pageSize") int pageSize);
    //insert
    @Insert("insert into coffee (name,price,create_time,update_time)values (#{name},#{price},now(),now())")
    @Options(useGeneratedKeys = true)
    int save(Coffee coffee) ;

    //select by id
    @Select("select * from coffee where id = #{id}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "price",property = "price"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime"),
    })
    Coffee findById(@Param("id") Long id);

    //select by name
    @Select("select * from coffee where name = #{name}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "price",property = "price"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime"),
    })
    Coffee findByName(@Param("name") String name);
    //select all
    @Select("select * from coffee where 1=1")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "price",property = "price"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime"),
    })
    ArrayList<Coffee> findAllCoffee();

    //update
    @Update("update coffee set price = #{price},update_time = now() where name = #{name}")
    int update(Coffee coffee);

    //delete
    @Delete("delete from coffee where name = #{name}")
    int delete(Coffee coffee);

    @Select("select count(*) from coffee")
    Long count();
}
